<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文件同步任务列表 - 同步平台</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.3/font/bootstrap-icons.css" rel="stylesheet">
    <link th:href="@{/css/style.css}" rel="stylesheet">
</head>
<body>
    <div class="container-fluid">
        <div class="row">
            <!-- Sidebar -->
            <nav id="sidebar" class="col-md-3 col-lg-2 d-md-block bg-light sidebar collapse">
                <div class="position-sticky pt-3">
                    <ul class="nav flex-column">
                        <li class="nav-item">
                            <a class="nav-link" th:href="@{/}">
                                <i class="bi bi-house"></i> 首页
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" th:href="@{/dashboard}">
                                <i class="bi bi-speedometer2"></i> 仪表盘
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" th:href="@{/server}">
                                <i class="bi bi-server"></i> 服务器管理
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" th:href="@{/database}">
                                <i class="bi bi-database"></i> 数据库管理
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" th:href="@{/sync/database}">
                                <i class="bi bi-arrow-left-right"></i> 数据库同步
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link active" th:href="@{/sync/file}">
                                <i class="bi bi-files"></i> 文件同步
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" th:href="@{/restore}">
                                <i class="bi bi-arrow-counterclockwise"></i> 数据库恢复
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" th:href="@{/task/log}">
                                <i class="bi bi-list-check"></i> 同步日志
                            </a>
                        </li>
                    </ul>
                </div>
            </nav>
            
            <!-- Main Content -->
            <main class="col-md-9 ms-sm-auto col-lg-10 px-md-4">
                <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
                    <h1 class="h2">文件同步任务列表</h1>
                    <div class="btn-toolbar mb-2 mb-md-0">
                        <a th:href="@{/sync/file/new}" class="btn btn-sm btn-primary">
                            <i class="bi bi-plus"></i> 新建任务
                        </a>
                    </div>
                </div>
                
                <div class="table-responsive">
                    <table class="table table-striped table-sm">
                        <thead>
                            <tr>
                                <th>任务名称</th>
                                <th>源服务器</th>
                                <th>目标服务器</th>
                                <th>状态</th>
                                <th>上次执行时间</th>
                                <th>上次执行结果</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr th:if="${tasks.isEmpty()}">
                                <td colspan="7" class="text-center">暂无文件同步任务</td>
                            </tr>
                            <tr th:each="task : ${tasks}">
                                <td th:text="${task.name}"></td>
                                <td th:text="${serverMap[task.sourceId]?.name ?: '未知'}"></td>
                                <td th:text="${serverMap[task.targetId]?.name ?: '未知'}"></td>
                                <td>
                                    <span th:if="${task.status == 1}" class="badge bg-success">启用</span>
                                    <span th:if="${task.status == 0}" class="badge bg-secondary">禁用</span>
                                </td>
                                <td th:text="${task.lastExecutionTime != null ? #temporals.format(task.lastExecutionTime, 'yyyy-MM-dd HH:mm:ss') : '未执行'}"></td>
                                <td>
                                    <span th:if="${task.lastExecutionResult == 1}" class="badge bg-success">成功</span>
                                    <span th:if="${task.lastExecutionResult == 0}" class="badge bg-danger">失败</span>
                                    <span th:if="${task.lastExecutionResult == null}" class="badge bg-secondary">未执行</span>
                                </td>
                                <td>
                                    <div class="btn-group btn-group-sm">
                                        <button th:attr="data-task-id=${task.id}" class="btn btn-primary btn-execute">执行</button>
                                        <a th:href="@{/sync/file/edit/{id}(id=${task.id})}" class="btn btn-info">编辑</a>
                                        <button th:attr="data-task-id=${task.id}" class="btn btn-danger btn-delete">删除</button>
                                    </div>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </main>
        </div>
    </div>
    
    <!-- Confirmation Modal -->
    <div class="modal fade" id="confirmModal" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">确认操作</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    确定要删除这个任务吗？此操作不可恢复。
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-danger" id="confirmDelete">确定删除</button>
                </div>
            </div>
        </div>
    </div>
    
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.6.3/dist/jquery.min.js"></script>
    <script th:src="@{/js/script.js}"></script>
    <script>
        $(document).ready(function() {
            // Delete task
            let taskIdToDelete = null;
            $('.btn-delete').click(function() {
                taskIdToDelete = $(this).data('task-id');
                $('#confirmModal').modal('show');
            });
            
            $('#confirmDelete').click(function() {
                if (taskIdToDelete) {
                    $.ajax({
                        url: '/sync/file/delete/' + taskIdToDelete,
                        type: 'POST',
                        success: function(result) {
                            if (result.success) {
                                location.reload();
                            } else {
                                alert('删除失败：' + (result.message || '未知错误'));
                            }
                        },
                        error: function() {
                            alert('删除失败：服务器错误');
                        },
                        complete: function() {
                            $('#confirmModal').modal('hide');
                        }
                    });
                }
            });
            
            // Execute task
            $('.btn-execute').click(function() {
                const taskId = $(this).data('task-id');
                const btn = $(this);
                
                btn.prop('disabled', true).html('<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> 执行中...');
                
                $.ajax({
                    url: '/sync/file/execute/' + taskId,
                    type: 'POST',
                    success: function(result) {
                        if (result.success) {
                            alert('执行成功：' + result.message);
                            location.reload();
                        } else {
                            alert('执行失败：' + (result.message || '未知错误'));
                            btn.prop('disabled', false).text('执行');
                        }
                    },
                    error: function() {
                        alert('执行失败：服务器错误');
                        btn.prop('disabled', false).text('执行');
                    }
                });
            });
        });
    </script>
</body>
</html> 